package com.yindian.cos.easy.security.authentication.verify;

import com.yindian.cos.easy.security.authentication.AbstractAuthentication;
import com.yindian.cos.easy.security.exception.AuthenticationException;
import com.yindian.cos.easy.security.handler.AuthenticationFailureHandler;
import org.springframework.util.AntPathMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截的抽象
 *
 * @author zhangchuanhao
 * @create 2018/11/22
 */
public abstract class AbstractVerifyAuthentication extends AbstractAuthentication {

    private final AntPathMatcher antPathMatcher = new AntPathMatcher();

    /**
     * 抽象验证方法,所有异常直接抛出，异常会进入AuthenticationFailureHandler
     *
     * @return
     * @see AuthenticationFailureHandler
     */
    public abstract void verify(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException;

    /**
     * 是否继续向下执行其他拦截器
     *
     * @param request
     * @return
     */
    public abstract boolean next(HttpServletRequest request);

    /**
     * 判断是否拦截或者放行该路径
     *
     * @param urls
     * @param requestUrl
     * @return
     */
    protected boolean interceptOrPass(String[] urls, String requestUrl) {
        Boolean flag = false;
        for (String url : urls) {
            if (antPathMatcher.match(url, requestUrl)) {
                flag = true;
                break;
            }
        }
        return flag;
    }
}
